import re

test = "https://www.zhihu.com/question/315244741/answer/61970sadf4697"

pattern = re.compile(r"https://www.zhihu.com/question/(\d+)/answer/(\d+)$")

pattern2 = re.compile(r"https://www.zhihu.com/question/(\d+)/")
match = re.match(pattern,test)
match2 = re.match(pattern2,test)

x= 1